import type { FollowType, ConsultOrderItem } from '@/types/consult'
import { type Ref, ref, onMounted } from 'vue'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import {
  followOrUnfollow,
  getPrescriptionPic,
  cancelOrder,
  deleteOrder
} from '@/services/consult'
import { OrderType } from '@/enums'
import {
  showFailToast,
  showSuccessToast,
  showImagePreview,
  type FormInstance
} from 'vant'
import type { CodeType } from '@/types/user'
import { sendCode } from '@/services/user'
import { onUnmounted } from 'vue'
// 封装逻辑  规范 useXxx 表示使用某功能
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  // 传递类型的时候 多个类型可以给少的类型
  // {id,likeFlag}  {id,likeFlag,c}
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    follow
  }
}

// 封装查看处方逻辑

export const useShowPrescription = () => {
  // 查看处方
  const showPrescription = async (id: string) => {
    const res = await getPrescriptionPic(id)
    showImagePreview([res.data.url])
  }
  return { showPrescription }
}

// 取消订单逻辑
export const useCancelOrder = () => {
  // 取消订单
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }

  return {
    loading,
    cancelConsultOrder
  }
}

// 删除订单逻辑

export const useDeleteOrder = (cb: () => void) => {
  const delLoading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      delLoading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      cb && cb()
    } catch (err) {
      showFailToast('删除失败')
    } finally {
      delLoading.value = false
    }
  }
  return {
    delLoading,
    deleteConsultOrder
  }
}

export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

// 发送验证码逻辑
export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const form = ref<FormInstance>()
  const time = ref(0)
  let timeId: number
  // 发送验证码
  const onSend = async () => {
    if (time.value > 0) return
    await form.value?.validate('mobile')
    // 手机号校验成功
    await sendCode(mobile.value, type)
    showSuccessToast('发送成功')
    // 健全 防止出现多个定时器
    time.value = 60
    clearInterval(timeId)
    // 开始倒计时
    timeId = window.setInterval(() => {
      time.value--
      if (time.value === 0) window.clearInterval(timeId)
    }, 1000)
  }
  onUnmounted(() => {
    clearInterval(timeId)
  })
  return {
    form,
    time,
    onSend
  }
}
